package simtools.xml;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.Writer;

import org.jdom.Document;
import org.jdom.Element;
import org.jdom.input.SAXBuilder;
import org.jdom.output.Format;
import org.jdom.output.XMLOutputter;

public class TesteClientPlacement {

	public static void mostrarDados(Element content) {
		try {
			Format format = Format.getPrettyFormat();
            format.setLineSeparator("\n");
            
			XMLOutputter out = new XMLOutputter(format);
			out.output(content, System.out);
			
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	private static void EscreverDados(Element content, OutputStream out) throws IOException {
		Writer wout = new OutputStreamWriter(out, "UTF8");
		wout.write("<?xml version=\"1.0\"?>\r\n");
		wout.write("<!DOCTYPE Placements SYSTEM \"ClientPlacement.dtd\">\r\n");
		
		Format format = Format.getPrettyFormat();
        format.setLineSeparator("\n");
        XMLOutputter outputter = new XMLOutputter(format);
        outputter.output(content, wout);
        
        wout.flush();
	}
	
	public static void main(String[] args) {
		String fileRouters  = "xml/scenario4/Routers.xml";
		String fileRequests = "xml/scenario4/Requests.xml";
		Document content;
		
		Balance balance = new Balance();
		
		
		OutputStream out = null;
		
		try {
			out = new FileOutputStream(
			"xml/scenario4/ClientPlacement.xml");
		} catch (FileNotFoundException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}

		try {
			SAXBuilder builder = new SAXBuilder();
			
			Document docRouter = builder.build(new File(fileRouters));
			//RouterXMLHandler router = new RouterXMLHandler(docRouter);
			
			builder = new SAXBuilder();
			Document docClients = builder.build(new File(fileRequests));
			//RequestsXMLHandler clients = new RequestsXMLHandler(docClients);
			
			content = balance.createTopology(docClients , docRouter);
			
			EscreverDados(content.getRootElement(), out);
			
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
